<script setup>
import GoodsItem from '@/components/GoodsItem.vue'
import { useBanner } from './composables/useBanner'
import { useCategory } from './composables/useCategory'
import { onBeforeRouteUpdate, useRoute } from 'vue-router'
import { onMounted, ref } from 'vue'
import { getGoodList, getSubCategoryListAPI } from '@/apis/category'

const { categoryData } = useCategory()
const { bannerList } = useBanner()
const route = useRoute()

// 获取传入的 id 参数
const categoryId = route.params.id
const goodList = ref([])
const getGood = async () => {
  const res = await getGoodList(categoryId)
  goodList.value = res.data.data
  console.log(goodList.value)
}
onMounted(() => {
  getGood()
})
onBeforeRouteUpdate((to) => {
  // 重新获取新的分类ID
  const newCategoryId = to.params.id
  // 调用API获取新数据
  getGoodList(newCategoryId).then((res) => {
    goodList.value = res.data.data
  })
})
</script>

<template>
  <div class="top-category">
    <div class="container m-top-20">
      <!-- 面包屑 -->
      <div class="bread-container">
        <el-breadcrumb separator=">">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item v-if="categoryData.name">{{
            categoryData.name
          }}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="goods-list">
        <goods-item
          v-for="good in goodList"
          :key="good.id"
          :good="good"
        ></goods-item>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }

  .sub-list {
    margin-top: 20px;
    background-color: #fff;

    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;

      li {
        width: 168px;
        height: 160px;

        a {
          text-align: center;
          display: block;
          font-size: 16px;

          img {
            width: 100px;
            height: 100px;
          }

          p {
            line-height: 40px;
          }

          &:hover {
            color: $xtxColor;
          }
        }
      }
    }
  }
  .goods-list {
    width: 990px;
    display: flex;
    flex-wrap: wrap;

    li {
      width: 240px;
      height: 300px;
      margin-right: 10px;
      margin-bottom: 10px;

      &:nth-last-child(-n + 4) {
        margin-bottom: 0;
      }

      &:nth-child(4n) {
        margin-right: 0;
      }
    }
  }

  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;

    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }

      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: space-around;
      padding: 0 40px 30px;
    }
  }
  .bread-container {
    padding: 25px 0;
  }
  .home-banner {
    width: 1240px;
    height: 500px;
    margin: 0 auto;

    img {
      width: 100%;
      height: 500px;
    }
  }
}
</style>
